package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dao.AddressDao;
import com.dao.SysUserDao;
import com.pojo.Address;
import com.pojo.SysUser;
import com.service.SysUserService;
import com.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysUserServiceImpl implements SysUserService {
    //注入SysUserDao
    @Autowired
    private SysUserDao sysUserDao;
    //注入AddressDao
    @Autowired
    private AddressDao addressDao;
    @Override
    public SysUser findUserByUsername(String username) {
        //首先构造QueryWrapper来进行条件的添加
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username",username);//相当于where id=1
        return sysUserDao.selectOne(wrapper);
    }

    @Override
    public Address findUserDefaultAddress(Integer id) {
        //首先构造QueryWrapper来进行条件的添加
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("u_id",id);//相当于where id=1
        wrapper.eq("value",1);
        return addressDao.selectOne(wrapper);
    }

    @Override
    public boolean updatename(SysUser sysUser) {
        return sysUserDao.updateById(sysUser)>0 ? true : false;
    }

    @Override
    public boolean updatepassword(SysUser sysUser) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username",sysUser.getUsername());//相当于where id=1
        SysUser sysUser1 = sysUserDao.selectOne(wrapper);
        //加密
        String password = MD5.encrypt(sysUser.getPassword());
        sysUser1.setPassword(password);
        return sysUserDao.updateById(sysUser1)>0 ? true : false;
    }

    @Override
    public boolean updatehead(SysUser sysUser) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username",sysUser.getUsername());//相当于where id=1
        SysUser sysUser1 = sysUserDao.selectOne(wrapper);
        sysUser1.setHeadUrl(sysUser.getHeadUrl());
        return sysUserDao.updateById(sysUser1)>0 ? true :false;
    }

    @Override
    public String findUrlByName(String username) {
       return   sysUserDao.findUrlByName(username);
    }

    //查找username是否重名
    @Override
    public boolean findSameUsername(String username) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username",username);//相当于where id=1
        List<SysUser> list = sysUserDao.selectList(wrapper);
        if(list.size()==0){
            return true;
        }
        return false;
    }
    //注册
    @Override
    public boolean regist(SysUser sysUser) {
        //加密
        String password = MD5.encrypt(sysUser.getPassword());
        sysUser.setPassword(password);
        return sysUserDao.insert(sysUser)>0 ? true : false;
    }

}
